
package com.android.fitnesscenter.view.server;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.ConfigProperties.RoleType;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class ServerNewUserActivity extends BaseActivity {

    @ViewInject(R.id.btn_ok)
    private Button btn_ok;

    @ViewInject(R.id.edtxt_username)
    private EditText edtxt_username;

    @ViewInject(R.id.edtxt_password)
    private EditText edtxt_password;

    @ViewInject(R.id.edtxt_question_1)
    private EditText edtxt_question_1;

    @ViewInject(R.id.edtxt_question_2)
    private EditText edtxt_question_2;

    @ViewInject(R.id.edtxt_question_3)
    private EditText edtxt_question_3;

    private String roleTypeStr = RoleType.MANAGER;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_user_layout);
        ViewUtils.inject(this);
        Intent intent = getIntent();
        if (intent != null) {
            roleTypeStr = intent.getStringExtra("role_type");
        }
        if (TextUtils.equals(RoleType.MANAGER, roleTypeStr)) {
            initTitleBar(getString(R.string.txt_modify_login));
            new ViewHelper(ServerNewUserActivity.this).showBTNDialog(getString(R.string.txt_first_server));
        } else if (TextUtils.equals(RoleType.OPREATER, roleTypeStr)) {
            initTitleBar(getString(R.string.txt_new_operater));
        } else if (TextUtils.equals(RoleType.CHECKER, roleTypeStr)) {
            initTitleBar(getString(R.string.txt_new_checher));
        }
    }

    @OnClick(R.id.btn_ok)
    public void btn_ok(View view) {
        String username = "";
        if (edtxt_username != null && edtxt_username.getText() != null) {
            username = edtxt_username.getText().toString();
        }
        if (TextUtils.isEmpty(username)) {
            ToastHelper.getInstance().showShortMsg("用户名为空!");
            return;
        } else if (username.contains(" ")) {
            ToastHelper.getInstance().showShortMsg("用户名不能含有空字符!");
            return;
        }

        String password = "";
        if (edtxt_password != null && edtxt_password.getText() != null) {
            password = edtxt_password.getText().toString();
        }
        if (TextUtils.isEmpty(password)) {
            ToastHelper.getInstance().showShortMsg("密码为空!");
            return;
        }
        String answer1 = "";
        if (edtxt_question_1 != null && edtxt_question_1.getText() != null) {
            answer1 = edtxt_question_1.getText().toString();
        }
        String answer2 = "";
        if (edtxt_question_2 != null && edtxt_question_2.getText() != null) {
            answer2 = edtxt_question_2.getText().toString();
        }
        String answer3 = "";
        if (edtxt_question_3 != null && edtxt_question_3.getText() != null) {
            answer3 = edtxt_question_3.getText().toString();
        }
        if (TextUtils.isEmpty(answer1) || TextUtils.isEmpty(answer2) || TextUtils.isEmpty(answer3)) {
            ToastHelper.getInstance().showShortMsg("答案为空,请输入答案!");
            return;
        }
        String[] questions = getResources().getStringArray(R.array.questions);
        register(username, password, questions[0], answer1, questions[1], answer2, questions[2], answer3);
    }

    private void register(final String username, final String password, final String question1, String answer1,
            final String question2, String answer2, final String question3, String answer3) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        String imei = getIMEI();
        params.addBodyParameter("username", username);
        params.addBodyParameter("password", password);
        params.addBodyParameter("imei", imei);
        params.addBodyParameter("type", roleTypeStr);
        params.addBodyParameter("question1", question1);
        params.addBodyParameter("answer1", answer1);
        params.addBodyParameter("question2", question2);
        params.addBodyParameter("answer2", answer2);
        params.addBodyParameter("question3", question3);
        params.addBodyParameter("answer3", answer3);
        if (TextUtils.equals(roleTypeStr, RoleType.MANAGER)) {
            params.addBodyParameter("qx1", "1");
            params.addBodyParameter("qx2", "1");
            params.addBodyParameter("qx3", "1");
            params.addBodyParameter("qx4", "1");
            params.addBodyParameter("qx5", "1");
        } else if (TextUtils.equals(roleTypeStr, RoleType.OPREATER)) {
            params.addBodyParameter("qx1", "1");
        } else if (TextUtils.equals(roleTypeStr, RoleType.CHECKER)) {
            params.addBodyParameter("qx2", "1");
        }
        String url = ConfigProperties.SERVICE_URL + "register.do";
        Loger.e("url is " + url + " params is " + params + " roleTypeStr is " + roleTypeStr);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(ServerNewUserActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ServerNewUserActivity.this != null && !ServerNewUserActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        try {
                            String result = stringResponseInfo.result;
                            Loger.e("result is " + result);
                            JSONObject jsonObject = new JSONObject(result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    mSharePrefsHelper.setBoolean("is_first_login", false);
                                    mSharePrefsHelper.setString("server_username", username);
                                    if (TextUtils.equals(RoleType.MANAGER, roleTypeStr)) {
                                        backToServerLoginActivity();
                                        finish();
                                    } else {
                                        goToManagerActivity();
                                    }
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(ServerNewUserActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ServerNewUserActivity.this).showBTNDialog("修改登陆失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                if (ServerNewUserActivity.this != null && !ServerNewUserActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(ServerNewUserActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    private void goToManagerActivity() {
        Intent intent = new Intent(this, ServerManagerLoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void titlebarMethod(int id) {

    }
}
